package com.ruoyi.business.controller;

import java.util.List;


import org.apache.shiro.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.TPotentialCustomer;
import com.ruoyi.business.service.ITPotentialCustomerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 潜在客户Controller
 * 
 * @author ruoyi
 * @date 2021-07-26
 */
@Controller
@RequestMapping("/business/TPotentialCustomer")
public class TPotentialCustomerController extends BaseController
{
    private String prefix = "business/TPotentialCustomer";

    @Autowired
    private ITPotentialCustomerService tPotentialCustomerService;

    @GetMapping()
    public String TPotentialCustomer()
    {
        return prefix + "/TPotentialCustomer";
    }

    /**
     * 查询潜在客户列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TPotentialCustomer tPotentialCustomer)
    {
        startPage();
        if (SecurityUtils.getSubject().hasRole("shopOwner")) {

            List<TPotentialCustomer> list = tPotentialCustomerService.selectTPotentialCustomerList(tPotentialCustomer);
            return getDataTable(list);
        } else {

            List<TPotentialCustomer> list = tPotentialCustomerService.selectTPotentialCustomerByUser(tPotentialCustomer);
            return getDataTable(list);
        }


    }

    /**
     * 导出潜在客户列表
     */
    @Log(title = "潜在客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TPotentialCustomer tPotentialCustomer)
    {
        List<TPotentialCustomer> list = tPotentialCustomerService.selectTPotentialCustomerList(tPotentialCustomer);
        ExcelUtil<TPotentialCustomer> util = new ExcelUtil<TPotentialCustomer>(TPotentialCustomer.class);
        return util.exportExcel(list, "潜在客户数据");
    }

    /**
     * 新增潜在客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存潜在客户
     */
    @Log(title = "潜在客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TPotentialCustomer tPotentialCustomer)
    {
       int count = tPotentialCustomerService.selectTPotentialCustomerByPhone(tPotentialCustomer);
        if (count == 0) {
            return toAjax(tPotentialCustomerService.insertTPotentialCustomer(tPotentialCustomer));
        } else {

            return AjaxResult.success("手机号重复");
        }
    }

    /**
     * 修改潜在客户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TPotentialCustomer tPotentialCustomer = tPotentialCustomerService.selectTPotentialCustomerById(id);
        mmap.put("tPotentialCustomer", tPotentialCustomer);
        return prefix + "/edit";
    }

    /**
     * 修改保存潜在客户
     */
    @Log(title = "潜在客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TPotentialCustomer tPotentialCustomer)
    {
        return toAjax(tPotentialCustomerService.updateTPotentialCustomer(tPotentialCustomer));
    }

    /**
     * 删除潜在客户
     */
    @Log(title = "潜在客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tPotentialCustomerService.deleteTPotentialCustomerByIds(ids));
    }







}
